package com.example.lxc415billiards.controller;

import com.example.lxc415billiards.domain.OrderGoods;
import com.example.lxc415billiards.service.OrderGoodsService;
import com.example.lxc415billiards.untils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderGoods")
@Api(tags = "订单商品管理-李效丞0415")
public class OrderGoodsController {

    @Autowired
    private OrderGoodsService orderGoodsService;

    @ApiOperation("获取所有订单商品信息")
    @GetMapping("/getAllOrderGoods")
    public AjaxResult getAllOrderGoods() {
        List<OrderGoods> orderGoods = orderGoodsService.getAllOrderGoods();
        return AjaxResult.success(orderGoods);
    }

    @ApiOperation("根据ID获取订单商品信息")
    @GetMapping("/getOrderGoodsById/{id}")
    public AjaxResult getOrderGoodsById(@PathVariable int id) {
        OrderGoods orderGoods = orderGoodsService.getOrderGoodsById(id);
        return orderGoods != null ? AjaxResult.success(orderGoods) : AjaxResult.error("订单商品不存在");
    }

    @ApiOperation("新增订单商品")
    @PostMapping("/addOrderGoods")
    public AjaxResult addOrderGoods(@RequestBody OrderGoods orderGoods) {
        OrderGoods newOrderGoods = orderGoodsService.addOrderGoods(orderGoods);
        return AjaxResult.success(newOrderGoods);
    }

    @ApiOperation("根据ID删除订单商品")
    @DeleteMapping("/deleteOrderGoodsById/{id}")
    public AjaxResult deleteOrderGoodsById(@PathVariable int id) {
        orderGoodsService.deleteOrderGoodsById(id);
        return AjaxResult.success("删除成功");
    }

    @ApiOperation("修改订单商品信息")
    @PutMapping("/updateOrderGoods")
    public AjaxResult updateOrderGoods(@RequestBody OrderGoods orderGoods) {
        OrderGoods updatedOrderGoods = orderGoodsService.updateOrderGoods(orderGoods);
        return updatedOrderGoods != null ? AjaxResult.success(updatedOrderGoods) : AjaxResult.error("更新失败");
    }
}
